<?php
/**
 * User: Titan
 * Date: Sep 28, 2010
 * Time: 5:29:23 PM
 */

/**
 * The stages of the system's load and launch
 */
 
// This stage loads any necessary configuration data and other useful variables  
define('INITIALIZE_CONFIGURATION', 0);

// This stage creates a user session, which is useful for form verification
define('INITIALIZE_SESSION', 1);

// This stage loads the database connections and functions 
define('INITIALIZE_DATABASE', 2);

// This stage checks the request URI and loads the appropriate file for the request
define('INITIALIZE_PATH', 3);

// This stage loads any functions necessary for rendering output, creating forms, and handling 
// database objects
define('INITIALIZE_SYSTEM', 4);

// This stage loads any extra variables sent to the system via the query string and stores them
// for othere functions to use
define('INITIALIZE_FULL', 5);

/**
 * initialize()
 * 
 * Initialize the entire system for use.
 * 
 * @param mixed $phase
 * @return void
 */
function initialize($phase = INITIALIZE_FULL)
{
	$phase_stage = 0;
	
	$vars = array();
	while ($phase_stage <= $phase)
	{
		switch ($phase_stage)
		{
			case INITIALIZE_CONFIGURATION:
				// get settings from the settings.inc file
				initialize_configuration($vars);

				break;
			case INITIALIZE_SESSION:
				// initialize a session for the current user
				session_start();
				
				break;				
			case INITIALIZE_DATABASE:
				// initialize the database
				initialize_database($vars);

				break;
			case INITIALIZE_PATH:
				// initialize the path of the site
				initialize_path($vars);

				break;
			case INITIALIZE_SYSTEM:
				// initialize the main system
				initialize_system($vars);

				break;
			case INITIALIZE_FULL:
				// initialize any remaining things like form values, query params, etc.
				initialize_full($vars);
				
				break;
		}

		$phase_stage++;
	}
	
	return $vars;
}

/**
 * initialize_configuration()
 * 
 * sets up a few useful variables for use with forms, links, and other things
 * 
 * @return void
 */
function initialize_configuration(&$vars = array())
{
	global $base_url, $base_path, $base_root, $db_url;
	
	if (file_exists('./settings.inc'))
	{
		include_once ('./settings.inc');

		if (isset($db_url) && $db_url == 'mysql://username:password@localhost/databasename')
		{
			$db_url = null;
		}
	}
	else
	{
		$db_url = null;
	}

	if (isset($base_url))
	{
		$url_parts = parse_url($base_url);
		if (!isset($url_parts['path']))
		{
			$url_parts['path'] = '';
		}

		$base_path = $url_parts['path'] . '/';
		$base_root = substr($base_url, 0, strlen($base_url) - strlen($url_parts['path']));
	}
	else
	{
		$base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';

		$base_url = $base_root .= '://' . $_SERVER['HTTP_HOST'];

		if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/'))
		{
			$base_path = '/' . $dir . '/';
			$base_url .= $base_path;
		}
		else
		{
			$base_path = '/';
		}
	}
	
	$vars['base_path'] = $base_path;
	$vars['base_url'] = $base_url;
}

/**
 * initialize_database()
 * 
 * Load the database connections and query functions
 * 
 * @return void
 */
function initialize_database(&$vars = array())
{
	require_once ('./includes/database.inc');
	db_init($vars);
}

/**
 * initialize_path()
 * 
 * Checks the request URI and loads the appropriate file for the request
 * 
 * @return void
 */
function initialize_path(&$vars = array())
{
	include_once ('./includes/path.inc');
	path_init($vars, (defined('RUN_TESTS') ? RUN_TESTS : FALSE));
}

/**
 * initialize_system()
 * 
 * Loads functions that are critical to rendering output, creating forms, and creating database objects
 * 
 * @return void
 */
function initialize_system(&$vars = array())
{
	include_once ('./includes/system.inc');

	include_once ('./includes/render.inc');
	include_once ('./includes/form.inc');
	
	// main content handling files
	include_once ('./includes/dbobject.inc');
	include_once ('./includes/developer.inc');
	include_once ('./includes/feature.inc');
	include_once ('./includes/milestone.inc');
	include_once ('./includes/task.inc');
}

/**
 * initialize_full()
 * 
 * Loads any extra query string variables 
 * 
 * @param mixed $vars
 * @return void
 */
function initialize_full(&$vars = array())
{	
	parse_str($_SERVER['QUERY_STRING'], $query_string);
	unset($query_string['q']);
	if (!empty($query_string))
	{
		$vars['query'] = $query_string;
		
		if (isset($vars['query']['op']))
		{
			$GLOBALS['op'] = $vars['query']['op'];
			
			if (isset($vars['query']['subop']))
			{
				$GLOBALS['subop'] = $vars['query']['subop'];
			}
		}
	}
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{
		$form_state = form_process_submitted_form($_POST);
		
		$vars['form_state'] = $form_state;
	}
}

/**
 * system_set_message()
 * 
 * This function is included here because some files use it before system.inc is included 
 * 
 * An awesome function that enables error messages or status messages to be displayed to the user without stopping
 * execution in runtime or using output_buffering
 * 
 * @param mixed $message
 * @return
 */
function system_set_message($message = null, $type = 'default')
{
	static $messages = array('default' => array(), 'error' => array());

	if (isset($message))
	{
		$messages[$type][] = $message;
	}
	
	return $messages;
}
